import React, { useReducer } from "react";

const dispatchActions: React.Dispatch<actionType> = () => {};

let data: ContextData = { // 初始值
  name: "context",
};

export const Context = React.createContext({
  contextData: data,
  dispatch: dispatchActions,
}); // 添加默认值是为了能够ts智能提示

const actions = (state: ContextData, action: actionType): ContextData => {
  switch (action.type) {
    case "updateName":
      return {
        ...state,
        name: action.updateValue.name,
      };
    default:
      return state;
  }
};

export default ({ children }: { children: React.ReactNode }): JSX.Element => {
  const [contextData, dispatch] = useReducer(actions, data);
  return (
    <Context.Provider
      value={{
        contextData,
        dispatch,
      }}
    >
      {children}
    </Context.Provider>
  );
};
